package com.buyouchina.service.product.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.buyouchina.bean.product.Account;
import com.buyouchina.bean.product.Account.AccountAreaType;
import com.buyouchina.bean.product.Account.AccountType;
import com.buyouchina.service.base.DaoSupport;
import com.buyouchina.service.product.AccountService;

@Service
public class AccountServiceBean extends DaoSupport<Account> implements
		AccountService {

	String hql;

	public Account checkRecord(Integer productid, String provinceid, Date date) {
		List<Account> accounts = new ArrayList<Account>();
		if (provinceid != null) {
			hql = "select s from Account s "
					+ "where s.date=?1 and s.product.id=?2 and s.province.id=?3 ";
			accounts = list(hql, date, productid, Integer.parseInt(provinceid));
		} else {
			hql = "from Account s "
					+ "where s.product.id=?1 and s.date=?2 and s.province.id is null";
			accounts = this.list(hql, productid, date);
		}
		if (accounts.size() > 0) {
			return accounts.get(0);
		}
		return null;
	}

	public List<Account> list(Date date, AccountAreaType type,
			Integer provinceid) {
		if (provinceid != null) {
			hql = "from Account a where a.date=?1 and a.areaType=?2 and a.province.id=?3";
			return list(hql, date, type, provinceid);

		} else {
			hql = "from Account a where a.date=?1 and a.areaType=?2 ";
			return list(hql, date, type);
		}
	}

	/*
	 * public List<Account> listByProduct(Integer productid,AccountAreaType
	 * areaType,AccountType type,Date date,Province p){ if(p!=null){hql=
	 * "from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4 and s.province.id between ?5 and ?6"
	 * ; return list(hql,
	 * productid,areaType,type,date,p.getId(),ProvinceUtil.getLastIndexOfProvince
	 * (p.getId())); }hql=
	 * "from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4"
	 * ; return list(hql, productid,areaType,type,date); }
	 */
	public List<Account> list(Date startTime, Date endTime, Integer provinceid) {
		if (provinceid != null) {
			hql = "from Account a where a.date=?1 and a.areaType=?2 and a.province.id=?3";
			return list(hql, startTime, endTime, provinceid);

		} else {
			hql = "from Account a where a.date=?1 and a.areaType=?2 ";
			return list(hql, startTime, endTime);
		}
	}

	public List<Account> listByProduct(Integer productid,
			AccountAreaType areaType, AccountType type, Date date, Integer pid,
			Integer epid) {
		if (pid != null) {
			hql = "from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4 and s.province.id between ?5 and ?6";
			return list(hql, productid, areaType, type, date, pid, epid);
		}
		hql = "from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4";
		return list(hql, productid, areaType, type, date);
	}
	

}
